<template>
  <div class="dashboard-container">
    <el-card>
      <div class="app-container">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button
              icon="el-icon-plus"
              style="margin:20px 0 10px 0"
              type="primary"
              size="small"
              @click="showRoleDialog = true"
              >新增角色</el-button
            >
            <el-table border :data="tableData" style="width: 100%">
              <el-table-column type="index" label="序号" width="180">
              </el-table-column>
              <el-table-column prop="name" sortable label="角色名" width="180">
              </el-table-column>
              <el-table-column prop="description" sortable label="描述">
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="addPermission(row.id)"
                    >分配权限</el-button
                  >
                  <el-button size="mini" type="warning" @click="editEmp(row.id)"
                    >修改</el-button
                  >
                  <el-button size="mini" type="danger" @click="doDel(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <!-- 弹出框 -->
            <el-dialog
              :title="title"
              :visible.sync="showRoleDialog"
              @close="cancel"
            >
              <el-form ref="roleForm" :model="roleForm" :rules="rules">
                <el-form-item prop="name" label="角色名" label-width="120px">
                  <el-input
                    v-model="roleForm.name"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item
                  prop="description"
                  label="描述"
                  label-width="120px"
                >
                  <el-input
                    v-model="roleForm.description"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
              </div>
            </el-dialog>

            <!-- 分配权限弹窗 -->
            <el-dialog :visible.sync="showPerDialog" @close="cancelPer">
              <el-tree
                ref="tree"
                :data="treeData"
                show-checkbox
                node-key="id"
                default-expand-all
                :default-checked-keys="checkedKeys"
                :props="defaultProps"
              >
              </el-tree>
              <el-row>
                <el-button size="mini" type="primary" @click="savePer"
                  >确定</el-button
                >
                <el-button size="mini" @click="cancelPer">取消</el-button>
              </el-row>
            </el-dialog>

            <!-- 分页 -->
            <el-pagination
              @current-change="getRoleList"
              :current-page.sync="pageInfo.page"
              :page-size="pageInfo.pagesize"
              layout="total, prev, pager, next"
              :total="pageInfo.total"
              style="margin:10px auto;text-align:center"
            >
            </el-pagination>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second" label-position="right">
            <el-form
              label-position="left"
              label-width="80px"
              style="margin:0 auto;width:50%"
              :model="companyForm"
            >
              <el-form-item label="企业名称">
                <el-input disabled v-model="companyForm.name"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  v-model="companyForm.companyAddress"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  disabled
                  v-model="companyForm.companyPhone"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled v-model="companyForm.mailbox"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input disabled v-model="companyForm.remarks"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  getRoleList,
  delRoleById,
  addRole,
  editEmp,
  getRoleById,
  getCompanyDetail
} from "@/api/setting";

import { getPermissionList } from "@/api/permission";
import { setPerToRole } from "@/api/setting";
import { transToTreeData } from "@/utils";

export default {
  name: "setting",
  data() {
    return {
      rules: {
        name: { required: true, message: "请输入角色名", trigger: "blur" },
        description: { required: true, message: "请输入描述", trigger: "blur" }
      },
      activeName: "first",
      showRoleDialog: false,
      companyForm: {},
      roleForm: {},
      tableData: [],
      // ---------权限弹窗的数据---------
      // 权限绑定的树状数据
      treeData: [],
      // 选中的权限
      checkedKeys: [],
      currentRoleId: null,
      showPerDialog: false,
      defaultProps: {
        children: "children",
        label: "name"
      },
      pageInfo: {
        page: 1,
        pagesize: 2,
        total: 0
      }
    };
  },
  methods: {
    // 分配权限按钮
    async addPermission(id) {
      let res = await getRoleById(id);
      this.checkedKeys = res.data.permIds;
      let res2 = await getPermissionList();
      this.treeData = transToTreeData(res2.data, "0");
      this.currentRoleId = id;
      this.showPerDialog = true;
    },
    // 权限弹窗点击确认事件
    async savePer() {
      let ids = this.$refs.tree.getCheckedKeys();
      await setPerToRole({ id: this.currentRoleId, permIds: ids });
      this.$message.success("分配权限成功！！！");
      this.showPerDialog = false;
    },
    // 取消事件按钮
    cancelPer() {
      this.treeData = [];
      this.checkedKeys = [];
      this.showPerDialog = false;
    },
    // 获取角色列表
    async getRoleList(page) {
      let res = await getRoleList(this.pageInfo);
      this.tableData = res.data.rows;
      this.pageInfo.total = res.data.total;
    },
    // 删除角色
    async doDel(id) {
      try {
        await this.$confirm("确定要删除？");
        await delRoleById(id);
        // 删除角色后，当当前的数据为最后一天是
        if (this.empData.length == 1 && this.pageInfo.page != 1) {
          this.pageInfo.page--;
        }
        this.getRoleList();
      } catch (error) {
        this.$message.error("删除失败");
      }
    },
    // 确定按钮
    save() {
      this.$refs.roleForm.validate(async v => {
        if (v) {
          if (this.roleForm.id) {
            await editEmp(this.roleForm);
          } else {
            await addRole(this.roleForm);
          }
          this.getRoleList();
          this.$message.success(this.title + "成功！！");
          this.showRoleDialog = false;
        } else {
          this.$message.error(this.title + "失败！！");
        }
      });
    },
    // 取消按钮
    cancel() {
      this.roleForm = {};
      this.$refs.roleForm.resetFields();
      this.showRoleDialog = false;
    },
    // 编辑按钮
    async editEmp(id) {
      let res = await getRoleById(id);
      this.roleForm = res.data;
      this.showRoleDialog = true;
    },
    async getCompanyDetail() {
      let res = await getCompanyDetail(this.$store.getters.companyId);
      this.companyForm = res.data;
    }
  },
  created() {
    this.getRoleList();
    this.getCompanyDetail();
  },
  computed: {
    title() {
      return this.roleForm.id ? "编辑角色" : "新增角色";
    }
  }
};
</script>

<style lang="scss" scoped>
.el-tabs__content {
  padding: 10px 10px 10px 20px;

  .el-table__header {
    th {
      background-color: #fafafa;
    }
  }
}
</style>
